/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components.TestGroup={
	SetGroupName:function(name)
	{
		this.C.groupName.text(name);
	},
	AddTestItem:function(item)
	{
		this._AddComponent(item);
		this.C.content.append(item);
	}
}
jsForms.Templates.TestGroup='<div>'+
	'<div class="testGroupName" name="groupName"/>'+
	'<div class="testGroupContent" name="content"/>'+
'</div>';
jsForms.Components.TestItem={
	SetName:function(name)
	{
		this.C.itemName.text(name);
	},
	StopRunTest:function()
	{
		var text = this.C.bStopRun.GetText();
		if (text == 'Stop')
		{
			this.StopTest();
		}else
		{
			this.RunTest();
		}
	},
	StopTest:function()
	{
		this.C.bStopRun.SetText('Run');
		if (this.P.Test.timeout != undefined) {
			clearTimeout(this.P.Test.timeout)
			delete(this.P.Test.timeout);
		}
	},
	RunTest:function()
	{
		this.C.bStopRun.SetText('Stop');
		var test = this.P.Test;
		if (!test.callback) {
			return;
		}
		test.allTime = 0;
		test.maxTime = -1;
		test.minTime = 999999;
		test.repeatCount = 0;
		test.repeatMaxCount = Tests.Options.CountTestRepeat;
		test.count = Tests.Options.Count;
		var progress = this.C.progress;
		progress.SetMin(0);
		progress.SetValue(0);
		progress.SetMax(test.repeatMaxCount);
		var testItem = this;
		function processCallback()
		{
			testItem.C.scores.text(	Tests.GetStatistic(test));
			testItem.C.progress.SetValue(test.repeatCount);
			if (test.repeatCount < test.repeatMaxCount)
			{
				test.timeout = setTimeout(testExec,100);
			}else
			{
				testItem.StopTest();
			}
		}
		function testExec()
		{
			var time = new Date().valueOf();
			test.callback(Tests.Options);
			time = new Date().valueOf() - time;
			test.allTime += time;
			if (test.maxTime < time) {
				test.maxTime = time;
			}
			if (test.minTime > time) {
				test.minTime = time;
			}
			test.repeatCount++;
			test.timeout = setTimeout(processCallback, 100,testItem);
		}
		test.timeout = setTimeout(testExec,100);
	},	
	SetTest:function(test)
	{
		this.P.Test = test;
		if (test.callback) {
			this.SetName(test.name + ':' + test.description);
		}
		else {
			this.SetName(test.name + ':' + test.description + ' - No callback');
		}
		this.C.scores.text(	Tests.GetStatistic(test));
	}
	
};
jsForms.Templates.TestItem='<div class="testItem">'+
	'<div class="testItemHeader">'+
		'<div name="itemName" class="testItemName"/>'+
		'<component type="Button" name="bStopRun">'+
			'<properties>'+
				'<key name="Text" value="Run"/>'+
			'</properties>'+
			'<events>'+
				'<key name="onClick" value="StopRunTest"/>'+
			'</events>'+
		'</component>'+
		'<component type="ProgressBar" name="progress" class="testItemProgress"/>'+
	'</div>'+
	'<div name="scores"/>'+
'</div>';
var Tests={
	Options:{
		Count:1000,
		CountTestRepeat:100,
		BlankText:'TEST text BlAnK',
		OnlyTotal: true
	},
	console:{
		log:function(){},
		error:function(){}
	},
	List:{},
	PushTest:function(category,name,description,callback)
	{
		if (!Tests.List[category])
		{
			Tests.List[category]=[];
		}
		Tests.List[category].push(
		{name:name,description:description,callback:callback});
	},
	GetStatistic:function(test)
	{
		function VPO(value)
		{
			return (value / test.count).toFixed(4);
		}
		if (test.repeatCount == undefined)
		{
			return '- no -';
		}
		return 'Rt:'+test.repeatCount+' R:'+test.count+', T:' + (test.allTime/test.repeatCount).toFixed(0) + ' ms' 
		+ ' [min:'+test.minTime+', max:'+test.maxTime+' ms] '
		+ ' [min:'+VPO(test.minTime)+' avr:' 
				+VPO(test.allTime / test.repeatCount)+ ', max:'+VPO(test.maxTime)+' ms]'
				+' delta='+(test.maxTime-test.minTime).toFixed(0);
	},
	
	CreateTestTable:function(content)
	{
		for (var i in Tests.List) {
			var group = jsForms.CreateComponent('TestGroup').hide().appendTo(content);
			group.SetGroupName(i);
			var cat = Tests.List[i];
			for (var ii = 0; ii < cat.length; ii++) {
				var item = jsForms.CreateComponent('TestItem');
				item.SetTest(cat[ii]);
				group.AddTestItem(item);
			}
			group.Init().show();
		}
		
	}
};

Tests.PushTest('Create','jQuery','$(Template), $(TAG.class[@atr="..."]), $().text()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(jsForms.Templates.Menu);
		var tmp1 = $('SPAN.menu-text[@name="text"]',tmp);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','jQuery','$(Template), $(.class), $().text()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(jsForms.Templates.Menu);
		var tmp1 = $('.menu-text',tmp);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});
Tests.PushTest('Create','jQuery','$(Template), $(.class), $().innerHTML',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(jsForms.Templates.Menu);
		var tmp1 = $('.menu-text',tmp);
		tmp1[0].innerHTML = opts.BlankText;
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','jQuery','$(Template), $(obj[0].childNodes[]), $().text()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(jsForms.Templates.Menu);
		var tmp1 = $(tmp[0].childNodes[1]);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','jQuery','$()[0].innerHtml, $(obj[0].childNodes[]), $().text()',function (opts)
{
	var kTml =  '<div class="menu"/>';
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(kTml);
		tmp[0].innerHTML = '<div class="menu-background"></div><span name="text" class="menu-text"/>';
		var tmp1 = $(tmp[0].childNodes[1]);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','jQuery','$()[0].innerHtml, $(TAG.class[@atr="..."]), $().text()',function (opts)
{
	var kTml =  '<div class="menu"/>';
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(kTml);
		tmp[0].innerHTML = '<div class="menu-background"/><span name="text" class="menu-text"/>';
		var tmp1 = $('SPAN.menu-text[@name="text"]',tmp);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','jQuery','$()[0].innerHtml, $(.class), $().text()',function (opts)
{
	var kTml =  '<div class="menu"/>';
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(kTml);
		tmp[0].innerHTML = '<div class="menu-background"/><span name="text" class="menu-text"/>';
		var tmp1 = $('.menu-text',tmp);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','jQuery','$()[0].innerHtml, $(.class), obj[0].innerHTML',function (opts)
{
	var kTml =  '<div class="menu"/>';
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(kTml);
		tmp[0].innerHTML = '<div class="menu-background"/><span name="text" class="menu-text"/>';
		var tmp1 = $('.menu-text',tmp);
		tmp1[0].innerHTML = opts.BlankText;
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','jQuery','$()[0].innerHtml, $(#id), obj[0].innerHTML',function (opts)
{
	var kTml =  '<div class="menu"/>';
	for (var i = 0; i < opts.Count; i++) {
		var tmp = $(kTml);
		tmp[0].innerHTML = '<div class="menu-background"/><span name="text" id="menu-text"/>';
		var tmp1 = $('#menu-text',tmp);
		tmp1[0].innerHTML = opts.BlankText;
		//cont.append(tmp);
	}
});



Tests.PushTest('Create','DOM','createElement(), $(TAG.class[@atr="..."]), $().text()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var root = document.createElement('div');
		root.className='menu';
		var tmp = document.createElement('div');
		root.appendChild(tmp);
		tmp.className = 'menu-background';
		
		tmp = document.createElement('span');
		root.appendChild(tmp);
		tmp.className='menu-text';
		tmp.setAttribute('name','text');
		
		var tmp = $(root);
		var tmp1 = $('SPAN.menu-text[@name="text"]',tmp);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','DOM','createElement(), $(.class), $().text()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var root = document.createElement('div');
		root.className='menu';
		var tmp = document.createElement('div');
		root.appendChild(tmp);
		tmp.className = 'menu-background';
		
		tmp = document.createElement('span');
		root.appendChild(tmp);
		tmp.className='menu-text';
		tmp.setAttribute('name','text');
		
		var tmp = $(root);
		var tmp1 = $('.menu-text',tmp);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','DOM','createElement()+innerHtml, $(obj.childNodes[]), $().text()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var root = document.createElement('div');
		root.className='menu';
		root.innerHTML = '<div class="menu-background"></div><span name="text" class="menu-text"/>';
		var tmp = $(root);
		var tmp1 = $(tmp[0].childNodes[1]);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});


Tests.PushTest('Create','DOM','createElement(), $(DOMElement), $().text()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var root = document.createElement('div');
		root.className='menu';
		var tmp = document.createElement('div');
		root.appendChild(tmp);
		tmp.className = 'menu-background';
		
		tmp = document.createElement('span');
		root.appendChild(tmp);
		tmp.className='menu-text';
		tmp.setAttribute('name','text');
		
		var tmp1 = $(tmp);
		var tmp = $(root);
		tmp1.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','DOM','createElement(), $(DOMElement), $().html()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var root = document.createElement('div');
		root.className='menu';
		var tmp = document.createElement('div');
		root.appendChild(tmp);
		tmp.className = 'menu-background';
		
		tmp = document.createElement('span');
		root.appendChild(tmp);
		tmp.className='menu-text';
		tmp.setAttribute('name','text');
		
		var tmp1 = $(tmp);
		var tmp = $(root);
		tmp1.html(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','DOM','createElement(), $(DOMElement), innerHTML',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var root = document.createElement('div');
		root.className='menu';
		var tmp = document.createElement('div');
		root.appendChild(tmp);
		tmp.className = 'menu-background';
		
		tmp = document.createElement('span');
		root.appendChild(tmp);
		tmp.className='menu-text';
		tmp.setAttribute('name','text');
		
		var tmp1 = $(tmp);
		var tmp = $(root);
		
		tmp1[0].innerHTML = opts.BlankText;
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','DOM','createElement(), $(DOMElement), innerHTML, cache element',function (opts)
{
	var doc = document;
	for (var i = 0; i < opts.Count; i++) {
		var root = doc.createElement('div');
		root.className='menu';
		var tmp = doc.createElement('div');
		root.appendChild(tmp);
		tmp.className = 'menu-background';
		
		tmp = doc.createElement('span');
		root.appendChild(tmp);
		tmp.className='menu-text';
		tmp.setAttribute('name','text');
		
		var tmp1 = $(tmp);
		var tmp = $(root);
		
		tmp1[0].innerHTML = opts.BlankText;
		//cont.append(tmp);
	}
});


Tests.PushTest('Create','jsForms','$().text()',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = jsForms.CreateComponent('Menu');
		tmp.Init();
		tmp.C.text.text(opts.BlankText);
		//cont.append(tmp);
	}
});

Tests.PushTest('Create','jsForms','SetText() - spec object method',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = jsForms.CreateComponent('Menu');
		tmp.Init();
		tmp.SetText(opts.BlankText);
		//cont.append(tmp);
	}
});
/*
Tests.PushTest('Parse','jsForms','Menu component',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = jsForms.ParseTemplate(jsForms.Templates.Menu);
		//var el1 = tmp.Children[0].Children[0];
	}
});
Tests.PushTest('Parse','jsForms','Menu component - new',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = jsForms.ParseTemplateNew('Menu');
	}
});
Tests.PushTest('Parse','DOM','Menu component',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var el = document.createElement('div');
		el.innerHTML = jsForms.Templates.Menu;
	}
});
Tests.PushTest('Parse','DOM','TestLongComponent',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var el = document.createElement('div');
		el.innerHTML = jsForms.FixIEComponentTemplate(jsForms.Templates.TestLongComponent);
	}
});
Tests.PushTest('Parse','jsForms','TestLongComponent',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = jsForms.ParseTemplate(jsForms.Templates.TestLongComponent);
	}
});
*/
/*
Tests.PushTest('Create','jsForms','Clear - TLC',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		jsForms.CleanCache('TestLongComponent');
		var tmp = jsForms.CreateComponent('TestLongComponent');
	}
});

Tests.PushTest('Create','jsForms','Clear - TLC - new',function (opts)
{
	var options = {
		VisualMethod:jsForms.CreateCache_CreateVisualNew
	};
	for (var i = 0; i < opts.Count; i++) {
		jsForms.CleanCache('TestLongComponent');
		var tmp = jsForms.CreateComponent('TestLongComponent',undefined,options);
	}
});

Tests.PushTest('Create','jsForms','Clear - TLC - old',function (opts)
{
	var options = {
		VisualMethod:jsForms.CreateCache_CreateVisualOld
	};
	for (var i = 0; i < opts.Count; i++) {
		jsForms.CleanCache('TestLongComponent');
		var tmp = jsForms.CreateComponent('TestLongComponent',undefined,options);
	}
});

Tests.PushTest('Parse','HTMLParser','TLC',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		HTMLParser(jsForms.Templates.TestLongComponent,{});
	}
});
Tests.PushTest('Parse','jsForms','TLC',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		jsForms.ParseTemplate(jsForms.Templates.TestLongComponent);
	}
});
Tests.PushTest('Parse','jsForms','TLC - new',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		jsForms.ParseTemplateM2(jsForms.Templates.TestLongComponent);
	}
});


Tests.PushTest('Parse','HTMLParser','Menu',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		HTMLParser(jsForms.Templates.Menu,{});
	}
});*/

Tests.PushTest('Parse','jsForms','Menu',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		jsForms.ParseTemplate(jsForms.Templates.Menu);
	}
});
Tests.PushTest('Parse','jsForms','Menu - M2',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		jsForms.ParseTemplateM2(jsForms.Templates.Menu);
	}
});
Tests.PushTest('Parse','jsForms','Menu - M3',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		jsForms.ParseTemplateM3(jsForms.Templates.Menu);
	}
});

Tests.PushTest('Parse','jsForms','Menu - M4',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		jsForms.ParseTemplateM4(jsForms.Templates.Menu);
	}
});

Tests.PushTest('Clean-Parse-Create','jsForms','Clear - Menu',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		jsForms.CleanCache('Menu');
		var tmp = jsForms.CreateComponent('Menu');
	}
});
/*
Tests.PushTest('Create','jsForms','Clear - Menu - new',function (opts)
{
	var options = {
		VisualMethod:jsForms.CreateCache_CreateVisualNew
	};
	for (var i = 0; i < opts.Count; i++) {
		jsForms.CleanCache('Menu');
		var tmp = jsForms.CreateComponent('Menu',undefined,options);
	}
});

Tests.PushTest('Create','jsForms','Clear - Menu - old',function (opts)
{
	var options = {
		VisualMethod:jsForms.CreateCache_CreateVisualOld
	};
	for (var i = 0; i < opts.Count; i++) {
		jsForms.CleanCache('Menu');
		var tmp = jsForms.CreateComponent('Menu',undefined,options);
	}
});

Tests.PushTest('Create','jsForms','Menu',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var tmp = jsForms.CreateComponent('Menu');
	}
});

Tests.PushTest('Create','jsForms','Menu - new',function (opts)
{
	var options = {
		VisualMethod:jsForms.CreateCache_CreateVisualNew
	};
	for (var i = 0; i < opts.Count; i++) {
		var tmp = jsForms.CreateComponent('Menu',undefined,options);
	}
});

Tests.PushTest('Create','jsForms','Menu - old',function (opts)
{
	var options = {
		VisualMethod:jsForms.CreateCache_CreateVisualOld
	};
	for (var i = 0; i < opts.Count; i++) {
		var tmp = jsForms.CreateComponent('Menu',undefined,options);
	}
});*/

/*
function GeneratorString(count, element,spliter)
{
	var l = [];
	for(var i = 0; i < count ; i++)
	{
		l.push(element);
	}
	return l.join(spliter);
}
var sample100 = GeneratorString(100,'a','');
var sample10 = GeneratorString(10,'a','');

var test100x100 = GeneratorString(100,sample100,'!');
var test1000x10 = GeneratorString(1000,sample10,'!');

Tests.PushTest('String','Split,For','100x100',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var items = test100x100.split('!');
		for(var ii in items)
		{
			var item = items[ii];
		}
	}
});
Tests.PushTest('String','RegEx,Substr','100x100',function (opts)
{
	
	for (var i = 0; i < opts.Count; i++) {
		var text = test100x100;
		var regExS = /\w*!/;
		while(text)
		{
			var item = text.match(regExS);
			if (item==null)
			{
				break;
			}
			text = text.substring(item[0].length);
		}
	}
});

Tests.PushTest('String','CharAt','100x100',function (opts)
{
	
	for (var i = 0; i < opts.Count; i++) {
		var text = test100x100;
		var count = text.length;
		var index =0 ;
		var sindex = 0;
		var hashMap={'!':true};
		while(index<count)
		{
			var c = text.charAt(index);
			if (hashMap[c])
			{
				var item = text.substring(sindex,index-sindex);
				sindex = index+1;
			}
			index++;
		}
	}
});

Tests.PushTest('String','Split,For','1000x10',function (opts)
{
	for (var i = 0; i < opts.Count; i++) {
		var items = test1000x10.split('!');
		for(var ii in items)
		{
			var item = items[ii];
		}
	}
});
Tests.PushTest('String','RegEx,Substr','1000x10',function (opts)
{
	
	for (var i = 0; i < opts.Count; i++) {
		var text = test1000x10;
		var regExS = /\w*!/;
		while(text)
		{
			var item = text.match(regExS);
			if (item==null)
			{
				break;
			}
			text = text.substring(item[0].length);
		}
	}
});

Tests.PushTest('String','CharAt','1000x10',function (opts)
{
	
	for (var i = 0; i < opts.Count; i++) {
		var text = test1000x10;
		var count = text.length;
		var index =0 ;
		var sindex = 0;
		var hashMap={'!':true};
		while(index<count)
		{
			var c = text.charAt(index);
			if (hashMap[c])
			{
				var item = text.substring(sindex,index-sindex);
				sindex = index+1;
			}
			index++;
		}
	}
});*/



function testCreateElement(cont){
	var root = document.createElement('component');
	root.setAttribute('t','text');
	var el1;var el2;
	el1 = document.createElement('properties');
	root.appendChild(el1);
	el2 = document.createElement('property');
	el2.setAttribute('name1','text');
	el1.appendChild(el2);
	$(root).appendTo(document.body);
}
//
